<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/WEB-INF/jsp/common.jsp"%>

<script type="text/javascript">
	var $mainModal = $("#main-loading");
	var article = {
		currentPage : 1,
		ajaxList : function(pageNum) {
			$mainModal.modal();
			$("#headBtnGroup").show();
			article.currentPage = pageNum;
			
			searchForm.pageNum.value = pageNum;
			searchForm.type.value = '${type}';
			
			$.ajax({
				url : '${basePath}/manage/article/column/${type}', 
				method: 'POST',
				data : $("#searchForm").serialize(), 
				dataType : 'html', 
				success : function(data) {
					$mainModal.modal("close");
					$("#tableBody").html(data);
				},
				error : function(data){
					$mainModal.modal("close");
					layer.msg("连接服务器时遇到错误！", {icon: 2});
				}
			}); 
		  
		},
		showSearchBox : function(){
			if($("#hideBar").css("display")=="none"){
				$("#hideBar").slideDown();
			}else{
				$("#hideBar").slideUp();
			}
		},
		search : function(){
			$mainModal.modal();
			searchForm.pageNum.value = 1;
			$.ajax({
				url : "${basePath}/manage/article/list", 
				method: 'POST',
				data : $("#searchForm").serialize(), 
				dataType : 'html', 
				success : function(data) {
					$mainModal.modal("close");
					$("#tableBody").html(data);
				},
				error : function(data){
					$mainModal.modal("close");
					layer.msg("连接服务器时遇到错误！", {icon: 2});
				}
			}); 
		},
		reset : function(){
			$("#searchForm")[0].reset();
		},
		preview : function(seqId){
			$mainModal.modal();
			$("#hideBar").hide();
			$("#headBtnGroup").hide();
			$.ajax({
				url : "${basePath}/manage/article/preview", 
				method: 'POST',
				data : {
					"seqId":seqId
				}, 
				dataType : 'html', 
				success : function(data) {
					$mainModal.modal("close");
					$("#tableBody").html(data);
				},
				error : function(data){
					$mainModal.modal("close");
					layer.msg("连接服务器时遇到错误！", {icon: 2});
				}
			}); 
		},
		edit : function(seqId){
			$mainModal.modal();
			$("#hideBar").hide();
			$("#headBtnGroup").hide();
			$.ajax({
				url : "${basePath}/manage/article/edit", 
				method: 'POST',
				data : {
					"seqId":seqId,
					"type": '${type}'
				}, 
				dataType : 'html', 
				success : function(data) {
					$mainModal.modal("close");
					$("#tableBody").html(data);
				},
				error : function(data){
					$mainModal.modal("close");
					layer.msg("连接服务器时遇到错误！", {icon: 2});
				}
			}); 
		},
		save : function(seqId){
			$modal.modal();
			// 更新
			if(seqId) {
				$.ajax({
					url : "${basePath}/manage/article/modify", 
					method: 'POST',
					data : $("#detailForm").serialize(), 
					dataType : 'html', 
					success : function(data) {
						$modal.modal("close");
						var result = article.dealSaveCallback(data);
						if(result == true){
							article.close();
						}
					},
					error : function(data){
						$modal.modal("close");
						layer.msg("连接服务器时遇到错误！", {icon: 2});
					}
				}); 
			}
			// 新增
			else {
				$.ajax({
					url : "${basePath}/manage/article/insert", 
					method: 'POST',
					data : $("#detailForm").serialize(), 
					dataType : 'html', 
					success : function(data) {
						$modal.modal("close");
						var result = article.dealSaveCallback(data);
						if(result == true){
							article.close();
						}
					},
					error : function(data){
						$modal.modal("close");
						layer.msg("连接服务器时遇到错误！", {icon: 2});
					}
				}); 
			}
		},
		del : function(seqId,articleName){
			layer.confirm('确定删除  '+articleName+' 这篇文章吗？', {
			  btn: ['确定','取消'] , icon:3 
			}, function(){
				$mainModal.modal();
				$.ajax({
					url : "${basePath}/manage/article/del", 
					method: 'POST',
					data : {"seqId":seqId}, 
					dataType : 'html', 
					success : function(data) {
						$mainModal.modal("close");
						if(data == "true" || data == true){
		  					layer.msg('文章删除成功', {icon: 1});
		  					article.ajaxList(article.currentPage);
						}
						else{
							layer.msg('文章删除失败', {icon: 2});
						}
					},
					error : function(data){
						$mainModal.modal("close");
						layer.msg("连接服务器时遇到错误！", {icon: 2});
					}
				}); 
			}, function(){});
		},
		close : function(){
			article.ajaxList(article.currentPage);
		},
		dealSaveCallback : function(data){
			if(data == "DM_ER"){
				layer.msg("更新或者新增文章时遇到错误！", {icon: 2});
			}
			else if(data == "AT_NP"){
				layer.msg("文章标题不能为空！", {icon: 2});
			}
			else if(data == "AC_CN"){
				layer.msg("文章内容不能为空！", {icon: 2});
			}
			else if(data == "AC_IN"){
				layer.msg("文章发布栏目不能为空！", {icon: 2});
			}
			else if(data == "ID_NP"){
				throw "修改时传入的主键为空";
				layer.msg("更新失败！", {icon: 2});
			}
			else {
				return true;
			}
		}
	};

	/** 
	 * 建立一个_this对象 
	 * 将查询列表的ajaxList方法赋值给$.ajaxList
	 * 公用分页插件将会使用这个方法分页
	 */
	var _this = {
		_ajaxList : article.ajaxList
	}

	$(document).ready(function() {
		article.ajaxList(1);
	});
</script>

<div class="admin-content-body">
	<div class="am-cf am-padding am-padding-bottom-0">
		<div class="am-fl am-cf">
			<strong class="am-text-primary am-text-lg">文章</strong> / <small>ARTICLE</small>
		</div>
	</div>
	<hr>
	<div class="main">
		<div class="am-g">
			<div class="am-u-sm-12" id="headBtnGroup">
				<button type="button" class="am-btn am-btn-default am-btn-xs am-fl"
					onclick="article.edit()">
					<span class="am-icon-plus"></span> 新增
				</button>

				<div class="am-fl" id="file-list"
					style="margin-top: 6px; margin-left: 10px"></div>
				<button type="button" class="am-btn am-btn-default am-btn-xs am-fr"
					onclick="article.showSearchBox()">
					<span class="am-icon-search"></span> 搜索
				</button>
			</div>
		</div>
		<div class="am-g" style="margin-top: 10px;">
			<div id="hideBar" style="display: none;">
				<%@ include file="search.jsp"%>
			</div>
			<div class="am-u-sm-12 am-u-md-12" id="tableBody"></div>
		</div>
	</div>
</div>

<div class="am-modal am-modal-loading am-modal-no-btn" tabindex="-1" id="main-loading">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">正在载入...</div>
    <div class="am-modal-bd">
      <span class="am-icon-spinner am-icon-spin"></span>
    </div>
  </div>
</div>